package ckw.network;

import java.sql.Time;
import java.util.ArrayList;
import ckw.database.DatabaseManager;

public class ValidationThread implements Runnable{
	
	// odpowiada za odblokowanie po 20 sekundach glosujacego 
	private static ArrayList<NonresidentValidationData> list = new ArrayList<NonresidentValidationData>();
	private DatabaseManager db = DatabaseManager.getInstance(); 
	private final int CZEKAJ = 20000;
	
	public void run() {
		
		while(true){
			ArrayList<NonresidentValidationData> tmp = new ArrayList<NonresidentValidationData>();
		
			for(NonresidentValidationData cc: list){
			
				Time time = new Time(System.currentTimeMillis());
				if (cc.getCzasDodania()+CZEKAJ<time.getTime()){
					System.out.println("ZERUJE WERYFIKACJE");
					db.setNonresidentVoterAsVoted(cc.getVoterID(), cc.getElectionName(), 0, cc.getKomisjaId());
					tmp.add(cc);
				}
			}
			for(NonresidentValidationData t: tmp){
				list.remove(t);
			}
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}

	public static synchronized ArrayList<NonresidentValidationData> getLista() {
		return list;
	}

	public static synchronized void addToList(NonresidentValidationData nonresidentValidationData) {
		ValidationThread.list.add(nonresidentValidationData);
	}
	
	public static synchronized void removeFromList(NonresidentValidationData item) {
		for(NonresidentValidationData cc: list){
			if(cc.getVoterID().equals(item.getVoterID()) && cc.getElectionName().equals(item.getElectionName()) 
					&& cc.getKomisjaId().equals(item.getKomisjaId())){
				list.remove(cc);
				break;
			}
		}
	}

}
